package com.byb.server.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.byb.server.service.ClaimsInstructionService;
import com.byb.server.web.rest.util.HeaderUtil;
import com.byb.server.web.rest.util.PaginationUtil;
import com.byb.server.service.dto.ClaimsInstructionDTO;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing ClaimsInstruction.
 */
@RestController
@RequestMapping("/api")
public class ClaimsInstructionResource {

    private final Logger log = LoggerFactory.getLogger(ClaimsInstructionResource.class);

    private static final String ENTITY_NAME = "claimsInstruction";

    private final ClaimsInstructionService claimsInstructionService;

    public ClaimsInstructionResource(ClaimsInstructionService claimsInstructionService) {
        this.claimsInstructionService = claimsInstructionService;
    }

    /**
     * POST  /claims-instructions : Create a new claimsInstruction.
     *
     * @param claimsInstructionDTO the claimsInstructionDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new claimsInstructionDTO, or with status 400 (Bad Request) if the claimsInstruction has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/claims-instructions")
    @Timed
    public ResponseEntity<ClaimsInstructionDTO> createClaimsInstruction(@RequestBody ClaimsInstructionDTO claimsInstructionDTO) throws URISyntaxException {
        log.debug("REST request to save ClaimsInstruction : {}", claimsInstructionDTO);
        if (claimsInstructionDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new claimsInstruction cannot already have an ID")).body(null);
        }
        ClaimsInstructionDTO result = claimsInstructionService.save(claimsInstructionDTO);
        return ResponseEntity.created(new URI("/api/claims-instructions/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /claims-instructions : Updates an existing claimsInstruction.
     *
     * @param claimsInstructionDTO the claimsInstructionDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated claimsInstructionDTO,
     * or with status 400 (Bad Request) if the claimsInstructionDTO is not valid,
     * or with status 500 (Internal Server Error) if the claimsInstructionDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/claims-instructions")
    @Timed
    public ResponseEntity<ClaimsInstructionDTO> updateClaimsInstruction(@RequestBody ClaimsInstructionDTO claimsInstructionDTO) throws URISyntaxException {
        log.debug("REST request to update ClaimsInstruction : {}", claimsInstructionDTO);
        if (claimsInstructionDTO.getId() == null) {
            return createClaimsInstruction(claimsInstructionDTO);
        }
        ClaimsInstructionDTO result = claimsInstructionService.save(claimsInstructionDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, claimsInstructionDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /claims-instructions : get all the claimsInstructions.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of claimsInstructions in body
     */
    @GetMapping("/claims-instructions")
    @Timed
    public ResponseEntity<List<ClaimsInstructionDTO>> getAllClaimsInstructions(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of ClaimsInstructions");
        Page<ClaimsInstructionDTO> page = claimsInstructionService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/claims-instructions");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /claims-instructions/:id : get the "id" claimsInstruction.
     *
     * @param id the id of the claimsInstructionDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the claimsInstructionDTO, or with status 404 (Not Found)
     */
    @GetMapping("/claims-instructions/{id}")
    @Timed
    public ResponseEntity<ClaimsInstructionDTO> getClaimsInstruction(@PathVariable Long id) {
        log.debug("REST request to get ClaimsInstruction : {}", id);
        ClaimsInstructionDTO claimsInstructionDTO = claimsInstructionService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(claimsInstructionDTO));
    }

    /**
     * DELETE  /claims-instructions/:id : delete the "id" claimsInstruction.
     *
     * @param id the id of the claimsInstructionDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/claims-instructions/{id}")
    @Timed
    public ResponseEntity<Void> deleteClaimsInstruction(@PathVariable Long id) {
        log.debug("REST request to delete ClaimsInstruction : {}", id);
        claimsInstructionService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/claims-instructions?query=:query : search for the claimsInstruction corresponding
     * to the query.
     *
     * @param query the query of the claimsInstruction search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/claims-instructions")
    @Timed
    public ResponseEntity<List<ClaimsInstructionDTO>> searchClaimsInstructions(@RequestParam String query, @ApiParam Pageable pageable) {
        log.debug("REST request to search for a page of ClaimsInstructions for query {}", query);
        Page<ClaimsInstructionDTO> page = claimsInstructionService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/claims-instructions");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    @GetMapping("/claims-instructions-all/{id}")
    @Timed
    public ResponseEntity<List<ClaimsInstructionDTO>> getAllbyProductId(@PathVariable Long id, @ApiParam Pageable pageable) {
        log.debug("REST request to search for a page of ClaimsInstructions for query {}", id);
        Page<ClaimsInstructionDTO> page = claimsInstructionService.getAll(id, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(String.valueOf(id), page, "/api/claims-instructions-all/{id}");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

}
